CKEDITOR.plugins.add('imageUploader', {
    requires: 'filetools',
    icons: 'imageUploader',
    beforeInit: function (editor) {
        if (!!!CKEDITOR.fileTools) {
            console.log("Please add the plugins fileTools and its requirements.")
        }
    },
    init: function (editor) {
        var fileDialog = $('<input type="file" accept="image/*" multiple />');
        fileDialog.on('change', function (e) {
            var fileTools = CKEDITOR.fileTools,
                uploadUrl = fileTools.getUploadUrl(editor.config, 'image'),
                files = e.target.files,
                img;
            for (var i in files) {
                if (files[i].constructor == File) {
                    var file = files[i];
                    var fileExtension = file.name.split('.').pop(); //后缀名
                    if ($.inArray(fileExtension, ['jpg', 'jpeg', 'png', 'gif', 'bmp']) >= 0) {
                        lrz(file, {width: 1440, quality: 0.8}).then(function (rst) {
                            var base2file = dataURLtoFile(rst.base64, rst.origin.name);
                            var formData = new FormData();
                            formData.append("upload", base2file);
                            $.ajax({
                                url: uploadUrl, /*接口域名地址*/
                                type: 'post',
                                data: formData,
                                contentType: false,
                                processData: false,
                                success: function (res) {
                                    var picInfo = JSON.parse(res);
                                    if(picInfo.uploaded==0){
                                        alert(picInfo.error.message);
                                    }else{
                                        img = editor.document.createElement('img');
                                        img.setAttribute('src', picInfo.url);
                                        img.setStyle('max-width', '100%');
                                        img.setStyle('alt', '');
                                        editor.insertElement(img);
                                    }
                                }
                            });
                        });
                    }
                }
            }
            // empty input
            fileDialog[0].value = "";
        });

        editor.ui.addButton('imageUploader', {
            label: '批量插入图片',
            command: 'openDialog',
            toolbar: 'insert'
        });

        editor.addCommand('openDialog', {
            exec: function (editor) {
                fileDialog.click();
            }
        });
    }
});

//将base64转换为文件  
function dataURLtoFile(dataurl, filename) {
    var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
        bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
    while (n--) {
        u8arr[n] = bstr.charCodeAt(n);
    }
    return new File([u8arr], filename, {type: mime});
}